package com.baymax.study.base.beanLifeCycle;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * MyInstantiationAwareBeanPostProcessor
 *
 * @author Baymax
 * @since 2024-03-23
 */
@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (StringUtils.equals(beanName, "myUserBean")) {
            System.out.println("[01]实例化前置处理器: InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation");
        }
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (StringUtils.equals(beanName, "myUserBean")) {
            System.out.println("[03]实例化后置处理器: InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation");
        }
        return true;
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if (StringUtils.equals(beanName, "myUserBean")) {
            System.out.println("[04]实例化后-初始化前，Bean属性Value值修改: InstantiationAwareBeanPostProcessor#postProcessProperties");
        }
        return null;
    }
}
